package com.situ.mall.web.config;

import com.category.spi.model.Category;
import com.mall.common.util.JsonResult;
import com.situ.mall.web.controller.MainController;
import com.situ.mall.web.service.CategoryFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@ControllerAdvice(assignableTypes = MainController.class)
public class MyControllerAdvice {
    private CategoryFeignService categoryFeignService;

    @Autowired
    public void setCategoryFeignService(CategoryFeignService categoryFeignService) {
        this.categoryFeignService = categoryFeignService;
    }

    @ModelAttribute("categories")
    public List<Category> header(Map<String, Object> map) {
        ModelAndView mav = new ModelAndView();
        //查询分类
        ResponseEntity<JsonResult<List<Category>>> re1 = this.categoryFeignService.tree();
        JsonResult<List<Category>> jr1 = re1.getBody();
        if (jr1 != null && jr1.isSuccess()) {
            List<Category> categories = jr1.getData();
            return categories.getFirst().getChildren();
        }
        return List.of();
    }
}
